package top.zhaoyuliu.logbook.server.connect.factory;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import top.zhaoyuliu.logbook.client.exception.LbException;
import top.zhaoyuliu.logbook.server.connect.service.DataService;

/**
 * Copyright © 2021 Arain. All rights reserved.
 *
 * @Title: HandlerFactory.java
 * @Prject: logbook-server
 * @Package: top.zhaoyuliu.logbook.server.connect.factory
 * @Description: <功能详细描述>
 * @author: Arain
 * @date: 2021年1月23日 下午1:18:00
 * @version: V1.0
 */
@Slf4j
@Component
public class HandlerFactory implements ApplicationContextAware {

	private final Map<Byte, DataService> ACTION_INSTANCE = new HashMap<>();

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		Map<String, DataService> map = context.getBeansOfType(DataService.class);
		map.values().forEach(e -> ACTION_INSTANCE.put(e.getType(), e));
	}

	public DataService exec(Byte actionType) {
		if (actionType == null) {
			log.error("类型为空");
			throw new LbException("类型为空");
		}
		return ACTION_INSTANCE.get(actionType);
	}

}
